import user from '@/api/user';
export default {
    state:{
        userinfo:{}, //用户信息
        uid:''
    },
    mutations:{
        SET_USERINFO(state,payload){
            state.userinfo = payload;  //设置用户信息
        },
        GET_USERINFO(state){
            let userinfo = window.localStorage.getItem('userinfo');
            userinfo = userinfo ? JSON.parse(userinfo) : userinfo;
            state.uid = userinfo.uid;
        }
    },
    actions:{
        async submitLogin({commit},{phone,checkcode}){
            let loginResult = await user.login({phone,checkcode});
            if(loginResult.code === 1){
                window.localStorage.setItem('token',loginResult.data.token);
                let userinfo = await user.getUserInfo();
                commit('SET_USERINFO',userinfo.data);
                window.localStorage.setItem('userinfo',JSON.stringify(userinfo.data));
            }
        }
    },
    namespaced:true
}

